package com.xx.xiawan.net;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fzh on 17-9-6.
 */

public class DGJsonOJectRequest extends JsonObjectRequest {
    private HashMap<String,String> mHeadMap = new HashMap<String,String>();
    public DGJsonOJectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }

    public DGJsonOJectRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(Method.POST, url, jsonRequest, listener, errorListener);
    }

    public void header(String name, String value) {
        mHeadMap.put(name, value);
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        if (mHeadMap != null && mHeadMap.size() > 0) {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Charset", "UTF-8");
            headers.put("Content-Type", "application/json");
            for(Map.Entry<String,String> entry:mHeadMap.entrySet()){
                headers.put(entry.getKey(), entry.getValue());
            }

            return headers;
        } else {
            return super.getHeaders();
        }
    }

}
